using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using SuperMarketManagement.DAO.NHibernate;
using SuperMarketManagement.Entity;

namespace SuperMarketManagement.Web.Controllers
{
    [Authorize(Roles = "admin,inventory-manager")]
    public class ProductController : Controller
    {
        //
        // GET: /Product/

        public ActionResult List()
        {
            DAOFactory factory = new DAOFactory();
            //select the category to be displayed
            Category catParent = null;
            if (Request["catid"] != null)
            {
                catParent = factory.CategoryDAO.GetById(Convert.ToInt64(Request["catid"]));
            }
            else
            {
                catParent = factory.CategoryDAO.GetRoot();
            }
            if (Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                if (Request["removeId"] != null)
                {
                    Product productToRemove = factory.ProductDAO.GetById(Convert.ToInt64(Request["removeId"]));
                    factory.ProductDAO.Delete(productToRemove);
                }
            }
            IList<Product> products = factory.ProductDAO.GetByCategory(catParent);
            ViewData["catParent"] = catParent;
            ViewData["products"] = products;
            return View();
        }

        public ActionResult Edit()
        {
            DAOFactory factory = new DAOFactory();
            long productId = Convert.ToInt64(Request["id"]);
            Product product = productId != 0 ? factory.ProductDAO.GetById(productId) : new Product();

            if (Request["catid"] != null)
            {
                product.Category = factory.CategoryDAO.GetById(Convert.ToInt64(Request["catid"]));
            }
            else if (productId == 0)
            {
                product.Category = factory.CategoryDAO.GetRoot();
            }

            if (Request["name"] != null)
            {//an update command was initiated
                product.Name = Convert.ToString(Request["name"]);
                product.Code = Convert.ToString(Request["code"]);
                product.Description = Convert.ToString(Request["description"]);
                product.Producer = Convert.ToString(Request["producer"]);
                product.Unit = Convert.ToString(Request["unit"]);
                product.CurrentSellingPrice = Convert.ToDouble(Request["currentSellingPrice"]);
            }

            if (Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase) && Request["save"] != null)
            {
                factory.ProductDAO.Save(product);
                return RedirectToAction("List", new { catid = product.Category.AutoId });
            }
            return View(product);
        }

        public ActionResult Info()
        {
            DAOFactory factory = new DAOFactory();
            long productId = Convert.ToInt64(Request["id"]);
            Product product = productId != 0 ? factory.ProductDAO.GetById(productId) : new Product();
            return View(product);
        }

        public ActionResult ViewAvailable()
        {
            DAOFactory factory = new DAOFactory();
            Product product = factory.ProductDAO.GetById(Convert.ToInt64(Request["pid"]));
            IList<ProductBlock> availables = factory.ProductBlockDAO.GetAvailablesByProductId(product.AutoId);
            ViewData["availables"] = availables;
            return View(product);
    
        }

        public ActionResult ViewPurchase()
        {
            DAOFactory factory = new DAOFactory();
            Product product = factory.ProductDAO.GetById(Convert.ToInt64(Request["pid"]));
            if (Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase) && Request["add"] != null)
            {
                double cost = Convert.ToDouble(Request["price"]);
                int quantity = Convert.ToInt32(Request["quantity"]);
                ProductBlock block = new ProductBlock(cost, DateTime.Now, quantity, quantity);
                product.AddBlock(block);
                factory.ProductDAO.Save(product);
            }
            return View(product);
  
        }

        public ActionResult ViewShrinkage()
        {
            DAOFactory factory = new DAOFactory();
            Product product = factory.ProductDAO.GetById(Convert.ToInt64(Request["pid"]));
            if (Request.RequestType.Equals("POST", StringComparison.OrdinalIgnoreCase))
            {
                if (Request["calc"] != null)
                {
                    double physicalQuantity = Convert.ToInt32(Request["physicalQuantity"]);
                    double diff = product.GetTotalAvailable() - physicalQuantity;
                    if (diff > 0)
                    {
                        IList<ProductBlock> blocks = factory.ProductBlockDAO.GetAvailablesByProductId(product.AutoId);
                        int index = blocks.Count - 1;
                        while (diff > 0)
                        {
                            ProductBlock block = blocks[index];
                            Shrinkage s;
                            if (block.AvailableUnits >= diff)
                            {
                                s = new Shrinkage(diff, block.Cost, DateTime.Now);
                                diff = 0;
                            }
                            else
                            {
                                s = new Shrinkage(block.AvailableUnits, block.Cost, DateTime.Now);
                                diff = diff - s.Quantity;
                            }
                            product.AddShrinkage(s);
                            block.AvailableUnits = block.AvailableUnits - s.Quantity;
                        }
                        factory.ProductDAO.Save(product);
                    }
                }
            }
            return View(product);
    
        }
    }
}
